﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class MarketController : BaseController
    {
        [Description("广告列表")]
        [ViewPage]
        public ActionResult GetList(MarketQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_AdConfig.Where(t => t.is_del == 0);
            if (!string.IsNullOrWhiteSpace(queryModel.ad_name))
                queryable = queryable.Where(t => t.ad_name == queryModel.ad_name);
            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.create_time >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.create_time < end_time);
            }

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.seq_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_AdConfig> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_AdConfig>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("广告配置")]
        [ViewPage]
        public ActionResult Add(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_AdConfig model = new ADZ_AdConfig();
            if (id > 0)
            {
                model = dbcontext.ADZ_AdConfig.FirstOrDefault(t => t.seq_id == id && t.is_del == 0);
            }
            return View(model);
        }

        [Description("广告配置")]
        [HttpPost]
        public ActionResult Add(ADZ_AdConfig model)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            //参数验证
            if (string.IsNullOrWhiteSpace(model.ad_name))
            {
                resMsg.Message = "广告名称不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            string linkUrl = model.link_url;
            if (!string.IsNullOrWhiteSpace(linkUrl))
            {
                linkUrl = model.link_url.ToLower();
                if (!(linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://")))
                {
                    resMsg.Message = "广告链接错误";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }
            }

            Tuple<bool, string> result = null;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (model.seq_id > 0)
                result = SysConfigBiz.Instance.EditAdConfig(model);
            else
                result = SysConfigBiz.Instance.AddAdConfig(model);

            resMsg.IsSuccess = result.Item1;
            resMsg.Message = result.Item2;
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("删除广告")]
        [HttpPost]
        public ActionResult ToDel(int id = 0)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var entity = dbcontext.ADZ_AdConfig.FirstOrDefault(t => t.seq_id == id);
            if (entity != null)
            {
                entity.is_del = 1;
                if (dbcontext.SaveChanges() > 0)
                {
                    resMsg.IsSuccess = true;
                    resMsg.Message = "操作成功";
                }
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}